package com.pickgliss.effect
{
	import com.pickgliss.toplevel.StageReferance;
	import com.pickgliss.utils.EffectUtils;
	import com.pickgliss.utils.ObjectUtils;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.display.GradientType;
	import flash.display.InterpolationMethod;
	import flash.display.MovieClip;
	import flash.display.Shape;
	import flash.display.SpreadMethod;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.filters.GradientGlowFilter;
	import flash.geom.ColorTransform;
	import flash.geom.GradientMatrix;
	import flash.geom.Matrix;

	public class ShinerAnimation extends BaseEffect
	{
		public static const SPEED:String = "speed";
		public static const INTENSITY:String = "intensity";
		public static const WIDTH:String = "width";
		public static const EFFECT:String = "effect";
		public static const COLOR:String = "color";
		public static const BLUR_WIDTH:String = "blurWidth";
		public static const IS_LOOP:String = "isLoop";
		
		public function ShinerAnimation($id:int)
		{
			_maskShape = new Shape();
			super($id);
		}

		private var _addGlowEffect:Boolean = true;
		private var _alphas:Array;
		private var _colors:Array;
		private var _currentPosition:Number;
		private var _endPosition:Number;
		private var _isFinish:Boolean;
		private var _glowBlurWidth:Number = 3;
		private var _glowColorName:String = EffectColorType.BLUE;
		private var _isLoop:Boolean = true;
		private var _maskHeight:Number;
		private var _maskShape:Shape;
		private var _maskWidth:Number;
		private var _percent:Array;
		private var _shineAnimationContainer:Sprite;
		private var _sourceBitmap:Bitmap;
		private var _shineBitmapContainer:Sprite;
		private var _shineIntensity:Number = 30;
		private var _shineMoveSpeed:Number = 15;
		private var _shineWidth:Number = 100;
		private var _startPosition:Number;
		
		override public function dispose():void
		{
			StageReferance.stage.removeEventListener(Event.ENTER_FRAME,onRenderAnimation);
			ObjectUtils.disposeObject(_shineAnimationContainer);
			ObjectUtils.disposeObject(_sourceBitmap);
			ObjectUtils.disposeObject(_shineBitmapContainer);
			_shineAnimationContainer = null;
			_sourceBitmap = null;
			_shineBitmapContainer = null;
			super.dispose();
		}
		
		override public function init(target:DisplayObject, datas:Array):void
		{
			super.init(target,datas);
			var data:Object = datas[0];
			if(data){
				if(data[SPEED])_shineMoveSpeed = data[SPEED];
				if(data[INTENSITY])_shineIntensity = data[INTENSITY];
				if(data[WIDTH])_shineWidth = data[WIDTH];
				if(data[EFFECT])_addGlowEffect = data[EFFECT];
				if(data[COLOR])_glowColorName = data[COLOR];
				if(data[BLUR_WIDTH])_glowBlurWidth = data[BLUR_WIDTH];
				if(data[IS_LOOP])_isLoop = data[IS_LOOP];
			}
			image_shiner(_shineMoveSpeed, _shineIntensity, _shineWidth, _addGlowEffect, _glowColorName, _glowBlurWidth, _isLoop);
		}
		
		override public function play():void
		{
			super.play();
			DisplayObjectContainer(target).addChild(_shineAnimationContainer);
			StageReferance.stage.addEventListener(Event.ENTER_FRAME,onRenderAnimation);
		}
		
		override public function stop():void
		{
			super.stop();
			if(!_shineAnimationContainer.parent)return;
			_shineAnimationContainer.parent.removeChild(_shineAnimationContainer);
			StageReferance.stage.removeEventListener(Event.ENTER_FRAME,onRenderAnimation);
		}
		
		private function image_shiner(shine_move_speed:Number, shine_intensity:Number, shine_width:Number, add_glow_effect:Boolean, glow_color_name:String, glow_blur_width:Number, loop:Boolean):void 
		{
			_shineAnimationContainer = new Sprite();
			_shineBitmapContainer = new Sprite();
			_sourceBitmap = EffectUtils.creatMcToBitmap(target,0xff0000);
			_shineBitmapContainer.addChild(_sourceBitmap);
			_shineAnimationContainer.addChild(_shineBitmapContainer);
			EffectUtils.imageShiner(_shineAnimationContainer,shine_intensity);
			EffectUtils.imageGlower(_shineBitmapContainer,1, glow_blur_width, 15, glow_color_name);
			linear_fade(shine_width, shine_move_speed, 60);
		}
		private function linear_fade (shine_width:Number, shine_move_speed:Number, mask_space:Number):void 
		{
			_maskShape.cacheAsBitmap = true;
			_shineAnimationContainer.cacheAsBitmap = true;
			_shineAnimationContainer.mask = _maskShape;
			_maskWidth = _shineAnimationContainer.width+mask_space;
			_maskHeight = _shineAnimationContainer.height+mask_space;
			_maskShape.x = _shineAnimationContainer.x-(mask_space/2);
			_maskShape.y = _shineAnimationContainer.y-(mask_space/2);
			_colors = [0xFFFFFF, 0xFFFFFF, 0xFFFFFF];
			_alphas = [0, 100, 0];
			_percent = [0, 127, 255];
			_startPosition = -(shine_width+mask_space);
			_currentPosition = _startPosition;
			_endPosition = _shineAnimationContainer.width+shine_width+mask_space;
			_shineAnimationContainer.addChild(_maskShape);
		}
		
		private function onRenderAnimation(event:Event):void
		{
			_maskShape.graphics.clear();
			var matrix:Matrix = new Matrix();
			matrix.createGradientBox(_shineWidth,_maskHeight,EffectUtils.toRadian(45),_currentPosition,0);
			_maskShape.graphics.beginGradientFill(GradientType.LINEAR, _colors, _alphas, _percent, matrix,SpreadMethod.PAD,InterpolationMethod.LINEAR_RGB);
			_maskShape.graphics.drawRect(0,0,_maskWidth,_maskHeight);
			_maskShape.graphics.endFill();
			if (_endPosition>_currentPosition) {
				_currentPosition += _shineMoveSpeed;
			} else {
				_isFinish = true;
				if (_isLoop) {
					_currentPosition = _startPosition;
				} else {
					StageReferance.stage.removeEventListener(Event.ENTER_FRAME,onRenderAnimation);
				}
			}
		}
		
	}
}